package com.hula.loan4u.activity;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.hula.loan4u.R;
import com.hula.loan4u.cons.AppCons;
import com.hula.loan4u.utils.ToastAlone;
import com.hula.loan4u.view.AppWebView;

import butterknife.BindView;
import butterknife.OnClick;


public class WebViewActivity extends BaseActivity {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.appWebView)
    AppWebView appWebView;
    String url;
    String htmlData;
    String titleStr;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        url = getIntent().getStringExtra(AppCons.WEBVIEW_URL);
        titleStr = getIntent().getStringExtra(AppCons.WEBVIEW_TITLE);
        htmlData = getIntent().getStringExtra(AppCons.WEBVIEW_DATA);
        if (!TextUtils.isEmpty(url)) {
            appWebView.loadUrl(url);
        } else if (!TextUtils.isEmpty(htmlData)) {
            appWebView.loadData(htmlData);
        } else {
            ToastAlone.show("找不到此网页");
            finish();
            return;
        }

        if (TextUtils.isEmpty(titleStr)) {
            title.setText("正在加载...");
        } else {
            title.setText(titleStr);
        }
    }


    @Override
    protected void setListener() {
        appWebView.setNewsWebViewCallBack(new AppWebView.NewsWebViewCallBack() {
            @Override
            public void loadSuccess() {
                if (appWebView != null & TextUtils.isEmpty(titleStr)) {
                    if (!TextUtils.isEmpty(appWebView.getTitle())) {
                        title.setText(appWebView.getTitle());
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (appWebView != null) {
            appWebView.destoryWebView();
        }
    }

    @Override
    public void onBackPressed() {
        if (appWebView != null) {
            if (!appWebView.back()) {
                super.onBackPressed();
            }
        } else {
            super.onBackPressed();
        }
    }

    @OnClick({R.id.left, R.id.right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left:
                onBackPressed();
                break;
            case R.id.right:
                finish();
                break;
        }
    }

}
